package view;

import java.util.ArrayList;
import java.util.Scanner;

import service.Constan;
import service.ProjectAPI;
import service.TimesheetAPI;
import service.implement.ProjectDAO;
import service.implement.TimesheetDAO;
import model.Project;

public class ProjectApp {
	Project pro=new Project();
	ProjectAPI pj=new ProjectDAO();
	TimesheetAPI ts=new TimesheetDAO();
	Scanner sc=new Scanner(System.in);
	public  void menuCapnhatPj(){
		System.out.println("1.Cap nhat theo id");
		System.out.println("2.Cap nhat theo nameProject");
		System.out.println("3.Thoat");
	}
	public  void menuDeletePj(){
		System.out.println("1.Xoa Project theo id");
		System.out.println("2.Xoa Project theo name");
		System.out.println("3.Thoat");
	}
	public static void menuTimkiemPj(){
		System.out.println("1.Tim kiem theo Id");
		System.out.println("2.Tim kiem theo ten");
		System.out.println("3.Thoat");
	}
	public void capNhatpjbyid(){
		try{

			if(pj.updateProject(pro.updateProject())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Update successfull");
			}
			else{
				System.out.println("Update Successfull");
			}
		}
		catch(NullPointerException e){
			System.out.println(" Vui long thu lai!");
		}
	}
	public void capNhatPjbyName(){
		System.out.println("Nhap nameProject can update:");
		String nameProject=sc.nextLine();
		while(nameProject.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.Nhap lai");
			nameProject=sc.nextLine();
		}

		if(pj.getProjectByName(nameProject).isEmpty()){
			System.out.println("Khong ton tai projectname");
		}else{
			ArrayList<Project> project=pj.getProjectByName(nameProject);
			System.out.println(Constan.DISPLAY_PJ);
			for(Project prt:project){
				System.out.println(prt);
			}
			capNhatpjbyid();
		}
	}
	public void inPj(){
		ArrayList<Project> proj= pj.getProject();
		System.out.println("=======================Danh sach Project=======================\n");
		System.out.println(Constan.DISPLAY_PJ);
		for (Project p:proj) {
			System.out.println("--------------------------------------------------------------------------");
			System.out.println(p);
		}
	}
	public void nhapPj(){
		try{
			if(pj.addProject(pro.inputProject())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Add successfull");
			}
			else{
				System.out.println("Add Successfull");
			}
		}catch(NullPointerException e){
			System.out.println("Them that bai.Vui long thu lai!");
		}
	}
	public void deletePjbyId(){
		System.out.println("Nhap id can xoa: ");
		int id=Integer.parseInt(sc.nextLine());
		if(pj.getProjectById(id)==null){
			System.out.println("Id khong ton tai");
		}else{
			System.out.println("Thong tin ve id da co");
			System.out.println(Constan.DISPLAY_PJ);
			ArrayList<Project> project=pj.getProject();
			Project projt=pj.getProjectById(id);
			System.out.println(projt);		
			System.out.println("Ban thuc su muon xoa");
			System.out.println("1.Co");
			System.out.println("2.Khong");
			int chon2=Integer.parseInt(sc.nextLine());
			if(chon2==1){
				if(ts.checkProjectid(id)){
					System.out.println("Thong tin project da duoc khai trong timesheet.Vui long xoa timesheet truoc va thu lai");
				}

				else if(pj.deleteProject(id)==Constan.SER_STATUS_SUCSESS){
					System.out.println("Delete Successfull");
				}
			}
			else
				System.out.println("Xoa khong thanh cong thu lai");
		}
	}
	public void searchPjbyId(){
		System.out.println("Nhap id can tim kiem");
		int id=Integer.parseInt(sc.nextLine());
		if(pj.getProjectById(id)==null){
			System.out.println("Khong ton tai id");
		}
		else{
			System.out.println(Constan.DISPLAY_PJ);
			Project projt=pj.getProjectById(id);
			System.out.println(projt);		
		}	
	}
	public void searchPjbyName(){
		System.out.println("Nhap name can tim kiem: ");
		String name=sc.nextLine();
		while(name.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.Nhap lai");
			name=sc.nextLine();
		}
		if(pj.getProjectByName(name).isEmpty()){
			ArrayList<Project> project=pj.getProjectByName(name);
			System.out.println(Constan.DISPLAY_PJ);
			for(Project show:project){
				System.out.println(show);
			}
		}else{
			System.out.println("Name khong ton tai");
		}

	}
	public void deletePjByName(){
		System.out.println("Nhap name can tim kiem: ");
		String name=sc.nextLine();
		while(name.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.Nhap lai");
			name=sc.nextLine();
		}
		if(pj.getProjectByName(name).isEmpty()){
			System.out.println("Name khong ton tai");
			return;
		}else{
			ArrayList<Project> project=pj.getProjectByName(name);
			System.out.println(Constan.DISPLAY_PJ);
			for(Project show:project){
				System.out.println(show);
			}
			deletePjbyId();
		}
		
	}

}
